<template>
  <HomePanel title="热门品牌" sub-title="国际经典 品质保证">
    <template v-slot:right>
      <a
        @click="left(0)"
        href="javascript:;"
        class="iconfont icon-angle-left prev"
        :class="{ disabled: index === 0 }"
      ></a>
      <a
        @click="right(1)"
        href="javascript:;"
        class="iconfont icon-angle-right next"
        :class="{ disabled: index === 1 }"
      ></a>
    </template>
    <div ref="target" class="box">
      <ul
        v-if="obj.length"
        class="list"
        :style="{ transform: `translateX(${-index * 1240}px)` }"
      >
        <!-- 每点一次的i 成上个1024   0 * 1024 = 0  0乘以任何数都等于0     1 * 1024 = 1024  -->
        <li v-for="i in obj" :key="i">
          <RouterLink to="/">
            <img :src="i.picture" alt="" />
          </RouterLink>
        </li>
      </ul>

      <div v-else class="skeleton">
        <XtxSkeleton
          class="item"
          v-for="i in 5"
          :key="i"
          animated
          bg="#e4e4e4"
          width="240px"
          height="305px"
        />
      </div>
    </div>
  </HomePanel>
</template>

<script>
import { hotBoard } from "@/api";
import { ref } from "vue";
import HomePanel from "@/components/home/homePanel.vue";
import { useLazyData } from "@/hooks"; // 引入我们的数据懒加载
export default {
  name: "HomeBrand",
  setup() {
    let obj = ref([]);
    let index = ref(0); // 每次切换的标记

    // 懒加载数据
    const { target, result } = useLazyData(hotBoard);
    // 右边的切换按钮
    function right(i) {
      index.value = i;
    }
    // 左边的按钮
    function left(i) {
      index.value = i;
    }
    return {
      obj: result,
      right,
      left,
      index,
      target,
    };
  },
  components: { HomePanel },
};
</script>

<style scoped lang="scss">
.home-panel {
  background: #f5f5f5;
}
.iconfont {
  width: 20px;
  height: 20px;
  background: #ccc;
  color: #fff;
  display: inline-block;
  text-align: center;
  margin-left: 5px;
  background: $xtxColor;
  &::before {
    font-size: 12px;
    position: relative;
    top: -2px;
  }
  &.disabled {
    background: #ccc;
    cursor: not-allowed;
  }
}
.box {
  display: flex;
  width: 100%;
  height: 345px;
  overflow: hidden;
  padding-bottom: 40px;
  .list {
    width: 200%;
    display: flex;
    transition: all 1s;

    li {
      margin-right: 10px;
      width: 240px;
      overflow: hidden;
      &:nth-child(5n) {
        margin-right: 0;
      }
      img {
        width: 240px;
        height: 305px;
      }
      &:hover {
        img {
          transform: scale(1.1);
          transition: all 0.8s;
        }
      }
    }
  }
}
</style>
